package cn._tedu.egaddress1112.controller;

import cn._tedu.egaddress1112.mapper.AddressMapper;
import cn._tedu.egaddress1112.pojo.dto.AddressAddParam;
import cn._tedu.egaddress1112.pojo.entity.Address;
import cn._tedu.egaddress1112.pojo.vo.AddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author tune_the last
 * @date 2025/11/12 - 18:42
 */

@RestController
@RequestMapping("/v1/address")
public class AddressController {

    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("/add")
    public String addAddress(@RequestBody AddressAddParam addressAddParam){
        Address address = new Address();
        System.out.println("啊啊啊啊" + addressAddParam);
        BeanUtils.copyProperties(addressAddParam, address);
        System.out.println("呃呃呃呃呃" + address);
        int i = addressMapper.insertAddress(address);
        if (i > 0){
            return "添加成功";
        }
        return "添加失败";
    }

    @GetMapping("/list")
    public List<AddressVO> getAddressList(){
        List<AddressVO> addressVOList = addressMapper.getAllAddressVO();
        return addressVOList;
    }

    @PostMapping("/del/{addressId}")
    public String deleteAddress(@PathVariable("addressId") Integer id){
        int i = addressMapper.deleteAddressById(id);
        if (i > 0){
            return "删除成功";
        }
        return "删除失败";
    }
}
